/*    
 * Copyright (c) 2013 ISoftStone, Inc.
 * All right reserved.
 *
 * 文件名：      AccountItemServiceImpl.java
 * 作者:     Jacky Wang
 * 创建日期： 2013-7-25 下午03:04:07
 * 版本：           
 *
 */
package com.isoftstone.industry.aaa.core.service.assist.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.isoftstone.industry.common.util.SNGenerator;
import com.isoftstone.industry.persistance.bean.AccountItem;
import com.isoftstone.industry.persistance.bean.AccountItemType;
import com.isoftstone.industry.persistance.bean.BillingCycle;
import com.isoftstone.industry.persistance.bean.PricingInfo;
import com.isoftstone.industry.persistance.bean.PricingInfoSnapshot;
import com.isoftstone.industry.persistance.bean.ProductInstance;
import com.isoftstone.industry.aaa.core.dao.assist.AccountItemDao;
import com.isoftstone.industry.aaa.core.service.assist.IAccountItemService;
import com.isoftstone.modules.core.persistence.IGenericDao;
import com.isoftstone.modules.core.service.impl.GenericServiceImpl;
import com.isoftstone.modules.core.utils.DateUtils;

/**
 * @author jacky
 */
@Service
public class AccountItemServiceImpl extends GenericServiceImpl<AccountItem, String> implements IAccountItemService
{
	@Resource
	private AccountItemDao			acctItemDao;

	@Override
	public IGenericDao<AccountItem, String> getDao()
	{
		return acctItemDao;
	}

	public AccountItem createAccountItem(AccountItemType acctItemType, ProductInstance prodInst, Double amount,
			PricingInfoSnapshot pricingInfoSnapshot, BillingCycle billingCycle)
	{

		//创建帐目实体
		AccountItem acctItem = new AccountItem();
		acctItem.setSn(SNGenerator.createAcctItemSN());
		acctItem.setProductInst(prodInst);
		acctItem.setAccountItemType(acctItemType);
		acctItem.setAmount(amount);
		acctItem.setPayableAmount(amount);
		acctItem.setPaidAmount(new Double(0.00));
		acctItem.setStatus(AccountItem.Status.NON_CHARGE_OFF);
		acctItem.setBillingCycle(billingCycle);
		Timestamp now = DateUtils.getCurrentTimestamp();
		acctItem.setStatusTime(now);
		acctItem.setCreatedTime(now);
		String chargeMode = pricingInfoSnapshot.getChargeMode();
		boolean prePaid = false;
		if(PricingInfo.ChargeMode.PRE_PAY.equals(chargeMode)){
			prePaid = true;
		}
		acctItem.setPrePaid(prePaid);
	//	acctItem.setPrePaidAmount(new Double(0.00));
		return acctItemDao.save(acctItem);
	}

	@Override
	public List<AccountItem> findNeedAccountingAccountItems(String userId, BillingCycle billingCycle)
	{
		Timestamp _start = DateUtils.str2Timestamp(DateUtils.date2String(billingCycle.getBeginDate()) + " 00:00:01");
		Timestamp _end = DateUtils.str2Timestamp(DateUtils.date2String(billingCycle.getEndDate()) + " 23:59:59");
		return acctItemDao.findByUserAndBillingCycleOrCreatedTimeBetween(userId, billingCycle.getId(), _start, _end);
	}

	@Override
	public void changeStatus(String acctItemId, String status)
	{
		acctItemDao.setStatus(acctItemId, status);
	}

	@Override
	public List<AccountItem> findNonChargeOffItemsByUser(String userId)
	{
		return acctItemDao.findByUserAndStatus(userId, AccountItem.Status.NON_CHARGE_OFF);
	}

	@Override
	public List<AccountItem> findPrePaidItems(String userId, String billingCycleId)
	{
		return acctItemDao.findByUserAndBillingCycleAndPrePaid(userId, billingCycleId, true);
	}

	@Override
	public List<AccountItem> findNeedPaidItems(String billId)
	{
		return acctItemDao.findByBillAndStatusAndPayableGreaterThan0(billId, AccountItem.Status.NON_CHARGE_OFF);
	}

//	@Override
//	public List<AccountItem> findNeedPrepaidItems(String userId)
//	{
//		return acctItemDao.findPrepaidByUserAndStatusAndPrepaidLessThanPayable(userId, AccountItem.Status.NON_CHARGE_OFF);
//	}

	@Override
	public List<AccountItem> findOwningPrepaidItems(String userId)
	{
		return acctItemDao.findOwningPrepaidItemsByUser(userId, AccountItem.Status.NON_CHARGE_OFF);
	}
}
